package com.hh.vietnameseproject.ui.recite

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.hh.vietnameseproject.R

class CardButtonView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {
    lateinit var icon:ImageView
    lateinit var wordText:TextView
    lateinit var wordStart:TextView

    private lateinit var mListener:()->Unit
    init {
        View.inflate(context, R.layout.card_button_view_layout,this)
        val typedArray= context.obtainStyledAttributes(attrs,R.styleable.CardButtonView)
        val mText = typedArray.getString(R.styleable.CardButtonView_text)
        val mDrawable = typedArray.getDrawable(R.styleable.CardButtonView_icon)
        val mLeftText: String? = typedArray.getString(R.styleable.CardButtonView_leftText)
        wordText = findViewById<TextView>(R.id.cardItemWord)
        icon = findViewById<ImageView>(R.id.cardItemImage)
        wordStart = findViewById<TextView>(R.id.cardItemStart)

        wordStart.text = mLeftText
        icon.setImageDrawable(mDrawable)
        wordText.text = mText

        wordText.setOnClickListener {
            mListener.invoke()
        }
        typedArray.recycle()
    }

    fun setIcon(resource:Int){
        icon.setImageResource(resource)
    }
    fun setText(text:String){
        wordText.text = text
    }
    fun setListener(listener: ()->Unit){
        mListener = listener
    }
    fun setButtonState(text: String,resource: Int){
        setText(text)
        setIcon(resource)
    }
    fun setEnable(enable:Boolean){
        wordText.isEnabled = enable
    }
    fun setSelect(isSelected:Boolean){
        wordText.isSelected = isSelected
        setEnable(!isSelected)
    }


}